using UnityEngine;
using System.Collections;

public class Enemy : MonoBehaviour
{
	public bool go_right = true;
	public float speed = 10f;
	
	private float direction;
	
	public void Start ()
	{
		renderer.material.SetColor ("_Color", Color.green);
		direction = go_right ? 1f : -1f;
		StartCoroutine (Walk ());
	}
	
	IEnumerator Walk ()
	{
		while (true)
		{
			transform.position = Vector3.MoveTowards(transform.position, transform.position + Vector3.right * direction, speed * Time.deltaTime);
			yield return null;
		}
	}
	
	void OnCollisionEnter (Collision collision)
	{
		if (collision.gameObject.layer == 8 || collision.gameObject.layer == 9 || collision.gameObject.layer == 10)
		{
			SnakeBody body = collision.gameObject.GetComponent <SnakeBody> ();
			
			if (body.WheelMode)
				Destroy (gameObject);
			else
				Application.LoadLevel (Application.loadedLevel);
		}
		else if (collision.gameObject.layer == 12 || collision.gameObject.layer == 31)
		{
			direction *= -1;
		}
	}
}
